package yxy.game.pm2.command.union.leader;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionLeaderMessage;
import yxy.game.pm2.module.player.PlayerHandle;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;

/** 联盟盟主 联盟申请列表 */
@MessageType(proto = Command.UnionApplyList, classtype = UnionLeaderMessage.UnionApplyListRequest.class)
public class CMD_UnionApplyList extends Command<UnionLeaderMessage.UnionApplyListRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionLeaderMessage.UnionApplyListRequest request) {
		String playerCode = messageSource.getTag();

		UnionLeaderMessage.UnionApplyListResponse.Builder response = UnionLeaderMessage.UnionApplyListResponse.newBuilder();

		// 管理员权限
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnionManager(entry);
		if (!result.equals(Result.UnionManager)) {
			send(messageSource, response, result);
			return;
		}
		UnionBean union = entry.getUnionBean();

		for (String code : union.getApplyList()) {
			try {
				response.addPlayers(new PlayerHandle().playerSimpleInfo(code));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, UnionLeaderMessage.UnionApplyListResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}